﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SYcwpb.Helpers;
using SYcwpb.Models;
using System.Data;

namespace SYcwpb.Access
{
    interface ILoginAccess
    {
        LoginResultType CheckUserExist(ref LoginModel loginModel);
        DataTable GetUserHeadImgInfo(string line);
        //MySql
        LoginResultType CheckUserExistMySql(ref LoginModel loginModel);
    }
    /// <summary>
    /// 简洁登录，去除原先需要输入职位和线路
    /// </summary>
    class SimpleLoginAccess:ILoginAccess
    {
        private SqlDataLayer sqlHelper = new SqlDataLayer();

        public LoginResultType CheckUserExist(ref LoginModel loginModel)
        {
            LoginResultType result = LoginResultType.Success;

            string sql_checkUser = "select count(*) from [system_user] S left join Position P on S.user_post = P.ID where "
                + " S.user_name = '" + loginModel.UserID + "' and "+
                "(p.post_type='部长' or p.post_type='派班员' or p.post_type='系统管理员' or p.post_type='电客车司机长' or p.post_type='督导员')";
                
            if (Convert.ToInt32(sqlHelper.GetSingle(sql_checkUser)) > 0)
            {
                StringBuilder sb = GlobalVariables.DecryptCode(loginModel.PasWrd);
                string sql_checkPasWrd = "select S.user_view_name , P.post_name ,S.user_line,S.user_name from [system_user] S left join Position P on S.user_post= P.Id where "
                    + "S.user_password = '" + sb.ToString() + "' and S.user_name = '" + loginModel.UserID + "'";
                DataTable dt = sqlHelper.SelectDt(sql_checkPasWrd);

                if (null != dt && dt.Rows.Count == 1)
                {
                    loginModel.PostName = dt.Rows[0]["post_name"].ToString();
                    loginModel.UserName = dt.Rows[0]["user_view_name"].ToString();
                    loginModel.Line = dt.Rows[0]["user_line"].ToString();
                    loginModel.ID = dt.Rows[0]["user_name"].ToString();
                }
                else
                    result = LoginResultType.PasWrdError;
            }
            else
                result = LoginResultType.NoOne;

            return result;
        }


        //MySql
        public LoginResultType CheckUserExistMySql(ref LoginModel loginModel)
        {
            LoginResultType result = LoginResultType.Success;
            //select count(*) from r_staff_info as rsi  LEFT JOIN  account_info as ai on ai.ai_username=rsi.en where ai_username='0169' and(rsi.post_name='部长' or rsi.post_name='派班员' or rsi.post_name='系统管理员' or rsi.post_name='电客车司机长' or rsi.post_name='督导员')
            string sql_checkUser = "select count(*) from r_staff_info as rsi  LEFT JOIN  account_info as ai on ai.ai_username=rsi.en where  "
                + " ai_username = '" + loginModel.UserID + "'";

            if (Convert.ToInt32(sqlHelper.GetSingleForMySql(sql_checkUser)) > 0)
           {
                StringBuilder sb = GlobalVariables.DecryptCode(loginModel.PasWrd);
            //select rsi.id,rsi.name,rsi.post_name,rsi.line from sso.r_staff_info as rsi  LEFT JOIN sso.account_info as ai on ai.ai_username=rsi.en where ai_username='1919'
                string sql_checkPasWrd = "select rsi.en,rsi.name,rsi.post_name,rsi.line from r_staff_info as rsi  LEFT JOIN  account_info as ai on ai.ai_username=rsi.en where "
                    + "ai.ai_password = '" + sb.ToString() + "' and ai.ai_username = '" + loginModel.UserID + "'";
                DataTable dt = sqlHelper.SelectDtFromMySql(sql_checkPasWrd);
               
                if (null != dt && dt.Rows.Count == 1)
                {
                  //  loginModel.PostName = dt.Rows[0]["post_name"].ToString();
                    loginModel.UserName = dt.Rows[0]["name"].ToString();
                  // loginModel.Line = dt.Rows[0]["line"].ToString();
                    loginModel.ID = dt.Rows[0]["en"].ToString();
                    string Sql = "select p.post_type,S.user_line from [system_user] S left join Position P on S.user_post = P.ID where S.user_name='" + loginModel.ID + "'";
                    DataTable postTypeTable = sqlHelper.SelectDt(Sql);
                    //if (Convert.ToInt32(sqlHelper.GetSingle(mySql)) <= 0)
                    //{
                    //    result = LoginResultType.NotInSqlServer;
                    //}
                    if (postTypeTable.Select().Length < 0)
                    {
                        result = LoginResultType.NotInSqlServer;
                    }
                    else
                    {
                        string postType;
                        postType = postTypeTable.Rows[0]["post_type"].ToString();
                        if (postType != "部长" && postType != "派班员" && postType != "系统管理员" && postType != "电客车司机长" && postType != "督导员")
                        {
                            result = LoginResultType.NoOne;
                        }
                        else
                        {
                            loginModel.PostName = postType;
                            loginModel.Line = postTypeTable.Rows[0]["user_line"].ToString();
                        }

                    }
                }
                else
                    result = LoginResultType.PasWrdError;
           }
            else
                result = LoginResultType.NoOne;

            return result;
        }

        public DataTable GetUserHeadImgInfo(string line)
        {
            DataTable dt = null;

            string sql = "select user_name, user_headImgMD5, user_img from [system_user] where user_dep_id = 7 and user_line = " + line;
            dt = sqlHelper.SelectDt(sql);

            return dt;
        }
    }

    //class LoginAccess:ILoginAccess
    //{
    //    private SqlDataLayer sqlHelper = new SqlDataLayer();

    //    public LoginResultType CheckUserExist(ref LoginModel loginModel)
    //    {
    //        LoginResultType result = LoginResultType.Success;

    //        string sql_checkUser = "select count(*) from [system_user] S left join Position P on S.user_post = P.ID where " 
    //            + "S.user_post ='" + loginModel.Depart + "' and S.user_name = '" + loginModel.UserID + "' and S.user_line = '" + loginModel.Line + "'";

    //        if ( Convert.ToInt32(sqlHelper.GetSingle(sql_checkUser)) > 0)
    //        {
    //            StringBuilder sb = GlobalVariables.DecryptCode(loginModel.PasWrd);
    //            string sql_checkPasWrd = "select S.user_view_name , P.post_name from [system_user] S left join Position P on S.user_post= P.Id where "
    //                + "S.user_password = '" + sb.ToString() + "' and S.user_name = '" + loginModel.UserID + "'";
    //            DataTable dt = sqlHelper.SelectDt(sql_checkPasWrd);

    //            if (null != dt && dt.Rows.Count == 1)
    //            {
    //                loginModel.PostName = dt.Rows[0]["post_name"].ToString();
    //                loginModel.UserName = dt.Rows[0]["user_view_name"].ToString();
    //            }
    //            else
    //                result = LoginResultType.PasWrdError;
    //        }
    //        else
    //            result = LoginResultType.NoOne;

    //        return result;
    //    }

    //    public DataTable GetUserHeadImgInfo(string line)
    //    {
    //        DataTable dt = null;

    //        string sql = "select user_name, user_headImgMD5, user_img from [system_user] where user_dep_id = 7 and user_line = " + line;
    //        dt = sqlHelper.SelectDt(sql);

    //        return dt;
    //    }
    //}

    public enum LoginResultType
    {
        NoOne = 0,
        PasWrdError = 1,
        Success = 2,
        //新添加
        NotInSqlServer=3,
    }
}
